/*** 本文件由CSMIS2.0-ModelCreater(UTF-8)工具辅助生成，版本1.0 ***/

package com.ce.sys.org.action;

import java.util.ArrayList;
import java.util.List;

import org.jsoup.helper.StringUtil;

import net.sf.json.JSONArray;

import com.ce.framework.constants.WebConstants;
import com.ce.framework.datamaintain.action.DataMaintainAction;
import com.ce.framework.exception.AppException;
import com.ce.framework.util.BeanAccessUtil;
import com.ce.framework.util.GeneratorUUID;
import com.ce.framework.util.LogUtil;
import com.ce.sys.dict.dao.po.SysDictItem;
import com.ce.sys.org.dao.po.SysOrg;
import com.ce.sys.org.dao.po.SysVOrg;
import com.ce.util.DbUtil;
import com.ce.util.DictUtil;
import com.ce.util.OrgUtil;
import com.ce.util.TreeNode;


/**
* 嫦娥软件有限公司 版权所有 2013
* 创建人：　andy
* 创建日期：2013-03-03
* 创建时间：16:13:55
* 功能描述：组织机构表 [SYS_T_ORG]更改时用的Action
* ==============================================
* 修改历史
* 修改人		修改时间		修改原因
*
* ==============================================
*/
public class SysOrgManagerAction extends DataMaintainAction 
{

	protected String openAddPage() throws AppException {
		// TODO Auto-generated method stub
		this.initOtherData(true);
		try{
			SysOrg org =(SysOrg)this.baselogic.getPOByPk("com.ce.sys.org.dao.po.SysOrg", this.getParameter("pid"));
			//this.setAttribute("orgLevel", this.getOrgLevels(org.getVcOrgLevel().intValue()));
			List dictList = DictUtil.getDictListByName("机构管理_机构类别");
			setAttribute("dictList", dictList);
			BeanAccessUtil.copyProperty(this.po, "vcParOrgid", this.getParameter("pid"));
			BeanAccessUtil.copyProperty(this.po, "vcId", GeneratorUUID.generateUUID());
			BeanAccessUtil.copyProperty(this.po, "vcDelFlag", new Integer(0));
			BeanAccessUtil.copyProperty(this.po, "nseq", DbUtil.getFirstIntSqlQuery("select max(n_seq) from sys_t_org where vc_par$orgid=?",
					new Object[]{this.getParameter("pid")})+1);
			//setAttribute("pOrgManProType",new Integer(this.getOrgManProType(org.getVcOrgLevel().intValue())));
		}catch(Exception e){
			LogUtil.logError("机构类型转换错误！");
		}
		return WebConstants.REQUEST_OPENADDPAGE_OPERATOR;
	}
	
	

	protected String openModifyPage() throws AppException {
		// TODO Auto-generated method stub
		List dictList = DictUtil.getDictListByName("机构管理_机构类别");
		setAttribute("dictList", dictList);
		return super.openModifyPage();
	}



	protected String look() throws AppException {
		// TODO Auto-generated method stub
		this.setAttribute("rootOrgId", OrgUtil.getRootOrgId());
		super.look();
		SysOrg org =(SysOrg)this.baselogic.getPOByPk("com.ce.sys.org.dao.po.SysOrg", this.getSelectId());
		SysDictItem dict = DictUtil.getDictItemByValueAndDictName(""+org.getVcType(), "机构管理_机构类别");
		if(dict!=null){
		   org.setVcType(dict.getVcItemName());
		   //BeanAccessUtil.copyBeanProperties(po, org);
		}
		setAttribute("orgLevel", ""+org.getVcOrgLevel());
		return WebConstants.REQUEST_LOOKDATA_OPERATOR;
	}



	protected String addItem() throws AppException {
		// TODO Auto-generated method stub
		if(this.validateSubmit()){		
			try{
				 //int orgLevel = Integer.parseInt(BeanAccessUtil.getFieldValue(po, "vcOrgLevel").toString());
			     //BeanAccessUtil.copyProperty(po, "vcManProType", new Integer(this.getOrgManProType(orgLevel)));
			     //BeanAccessUtil.copyProperty(po, "vcOrgTypeFlag", new Integer(this.getOrgTypeFlag(orgLevel)));
			    // BeanAccessUtil.copyProperty(po, "vcParOrgTypeFlag", new Integer(this.getPorgTypeFlag(orgLevel)));
			     //BeanAccessUtil.copyProperty(po, "vcType", new Integer(this.getType(orgLevel)));
			}catch(Exception e){
				LogUtil.logError("机构类型转换错误！");
			}
			String vcOrgLevel="";
			SysOrg org=(SysOrg)po;
			String vcParOrgId=org.getVcParOrgid();
			SysOrg parOrg=(SysOrg) this.baselogic.getPOByPk(SysOrg.class.getName(), vcParOrgId);
			org.setVcOrgLevel(parOrg.getVcOrgLevel()+1);
			this.baselogic.addPo(po,this.getAddList());
			return this.list();			
		}else{
			throw new AppException("提交数据验证失败，操作未成功！");
		}
	}



	protected String delItemForJS() {
		// TODO Auto-generated method stub
		int i= DbUtil.getFirstIntSqlQuery("select count(*) from SYS_V_ORG$DEP t where t.VC_PAR$ORGID ='"+this.getSelectId()+"'");
		if(i>0)
			return super.returnInfoForJS(false, "haveOrg");
		i= DbUtil.getFirstIntSqlQuery("select count(*) from sys_t_person t where t.vc_org$id ='"+this.getSelectId()+"' and t.vc_del$flag=0");
		if(i>0)
			return super.returnInfoForJS(false, "haveUser");
		return super.delItemForJS();
	}
	
	/**
	 * @author ： andy
	 * @since： Mar 4, 2012 12:37:41 PM
	 * 功能描述：根据机构类型(vcOrgLevel级别)获取下级机构类型列表
	 * 方法的参数和返回值
	 * @param level
	 * @return
	 * @throws AppException
	 * List 
	 * ==================================
	 * 修改历史
	 * 修改人        修改时间      修改原因及内容
	 *
	 * ==================================
	 */
	private List getOrgLevels(int level)throws AppException{
		List orgLevelList = new ArrayList();
		switch(level){
		case 1:
			orgLevelList.add(new TreeNode("2","市国家税务局领导"));
			orgLevelList.add(new TreeNode("3","市国家税务局科室"));//
			orgLevelList.add(new TreeNode("6","市稽查局"));
			break;
		case 2:
			orgLevelList.add(new TreeNode("2","市国家税务局领导"));
			break;
		case 3:
			orgLevelList.add(new TreeNode("4","科室领导"));
			orgLevelList.add(new TreeNode("5","科室"));
			break;
		case 4:
			orgLevelList.add(new TreeNode("4","科室领导"));
			break;
		case 5:
			orgLevelList.add(new TreeNode("5","科室"));
			break;
		case 6:
			orgLevelList.add(new TreeNode("7","市稽查局"));
			orgLevelList.add(new TreeNode("8","市稽查局科室"));
			orgLevelList.add(new TreeNode("9","稽查分局"));
			break;
		case 7:
			orgLevelList.add(new TreeNode("7","市稽查局领导"));
			break;
		case 8:
			orgLevelList.add(new TreeNode("8","市稽查局科室"));
			break;
		case 9:
			orgLevelList.add(new TreeNode("10","稽查分局科室"));
			break;
		}
		return orgLevelList;
	}
	
	/**
	 * @author ： andy
	 * @since： Mar 4, 2012 12:12:08 PM
	 * 功能描述：根据新机构类型获取原有机构类型
	 * 方法的参数和返回值
	 * @param orgLevel
	 * @return
	 * int 
	 * ==================================
	 * 修改历史
	 * 修改人        修改时间      修改原因及内容
	 *
	 * ==================================
	 */
	private int getOrgManProType(int orgLevel){
		int orgManProType  = 2;  //默认为局
		switch(orgLevel){
		case 6:
			orgManProType = 5;
			break;
		case 7:
			orgManProType = 5;
			break;
		case 8:
			orgManProType = 5;
			break;
		case 9:
			orgManProType = 6;
			break;
		case 10:
			orgManProType = 7;
			break;
		default:
		    orgManProType = 2;		
		}
		return orgManProType;
	}
	
	/**
	 * @author ： andy
	 * @since： Mar 4, 2012 12:13:11 PM
	 * 功能描述：根据机构类型获取机构类型标志
	 * 方法的参数和返回值
	 * @param orgLevel
	 * @return
	 * int 
	 * ==================================
	 * 修改历史
	 * 修改人        修改时间      修改原因及内容
	 *
	 * ==================================
	 */
	private int getOrgTypeFlag(int orgLevel){
		int orgTypeFlag  = 1;  //默认为局
		switch(orgLevel){
		case 1:
			orgTypeFlag = 1;
			break;
		case 6:
			orgTypeFlag = 1;
			break;
		case 9:
			orgTypeFlag = 2;
			break;
		case 10:
			orgTypeFlag = 2;
			break;
		default:
			orgTypeFlag = 3;		
		}
		return orgTypeFlag;
	}
	
	/**
	 * @author ： andy
	 * @since： Mar 4, 2012 12:13:31 PM
	 * 功能描述：根据机构类型获取上级机构标志
	 * 方法的参数和返回值
	 * @param orgLevel
	 * @return
	 * int 
	 * ==================================
	 * 修改历史
	 * 修改人        修改时间      修改原因及内容
	 *
	 * ==================================
	 */
	private int getPorgTypeFlag(int orgLevel){
		int pOrgTypeFlag  = 1;  //默认为局
		switch(orgLevel){
		case 4:
			pOrgTypeFlag = 3;
			break;
		case 5:
			pOrgTypeFlag = 3;
			break;
		case 10:
			pOrgTypeFlag = 2;
			break;
		default:
			pOrgTypeFlag = 1;		
		}
		return pOrgTypeFlag;
	}
	
	private Integer getType(int orgLevel){
		Integer vcType  = 2;  //默认为集团
		switch(orgLevel){
		case 1:
		   vcType = 2;
		   break;
		case 6:
			vcType = 5;
			break;
		case 9:
			vcType = 6;
			break;
		case 10:
			vcType = 7;
			break;
		default:
			vcType = null;	
		}
		return vcType;
	}
	
	public String getdList(){
		//段Id
		SysVOrg org = OrgUtil.getRootOrg();
		List dOrglist = new ArrayList();
		List cjOrglist = new ArrayList();
		JSONArray json = new JSONArray();
		if(org.getNid()==6){
			//当前根机构为段则只加载本段名称
			dOrglist.add(org);
		}else{
			dOrglist = DbUtil.executeHql("from SysVOrg po where po.vcParOrgid = '"+org.getVcId()+"' and po.nid = 6");
		}
		if(dOrglist!=null&&dOrglist.size()>0){
			SysVOrg firstOrg = (SysVOrg)dOrglist.get(0);
			cjOrglist = DbUtil.executeHql("from SysVOrg po where po.vcParOrgid = '"+firstOrg.getVcId()+"' and po.vcManProType = 6");
		}
		json.add(dOrglist);
		json.add(cjOrglist);
		
		return super.writeHTML(json.toString());
	}
	/*
	 * 稽查局
	 */
	public String getInspectionBureauList(){
		String id = this.getParameter("id");
		if(StringUtil.isBlank(id))
			id=OrgUtil.getRootOrgId();
		List orglist = DbUtil.executeHql("from SysVOrg po where po.vcType=1  and po.vcParOrgid = '"+id+"'");
		JSONArray json = new JSONArray();
		json.add(orglist);
		return super.writeHTML(json.toString());
	}
	/*
	 * 获取征管局
	 */
	public String getCollectionBureauList(){
		String id = this.getParameter("id");
		List orglist = DbUtil.executeHql("from SysVOrg po where  po.vcType = 1  and po.vcParOrgid = '"+id+"'");
		JSONArray json = new JSONArray();
		json.add(orglist);
		return super.writeHTML(json.toString());
	}
	

}

